//
//  HYYWelcomeViewController.swift
//  Hyy9Weibo
//
//  Created by Air on 16/3/29.
//  Copyright © 2016年 hyy. All rights reserved.
//

import UIKit
import SDWebImage

class HYYWelcomeViewController: UIViewController {

    //MARK: - 属性
    var iconViewBottomCons : NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        
        //在发送网络请求之前就先使用本地图片，有就用本地，没就默认
        if let urlString = HYYUserAccountModel.sharedInstance.userAccount?.avatar_large{
            let url = NSURL(string: urlString)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        //获取用户头像等信息，显示头像,需要等网络请求完了才加载
        HYYUserAccountModel.sharedInstance.loadUesrInfo { (error) in
            if error != nil{
                HYYPrint(msg: "请求用户信息出错了 : \(error)")
                return
            }
            //没有出错，给头像赋值，即使图片在本地有缓存也需要等到网络请求完才设置
            if let urlString = HYYUserAccountModel.sharedInstance.userAccount?.avatar_large{
                let url = NSURL(string: urlString)
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
    }
    
    private func prepareUI(){
        //1.添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //背景
        bkgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //头像
        //centerX
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        //底部
//        //记录底部约束
//        iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(iconViewBottomCons!)
        
        //欢迎label
        welcomeLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }
    
    //界面已经显示
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    //头像移动动画
    private func moveAnimation(){
//        iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height-160)

        //动画
//        UIView.animateWithDuration(HYYDefaultDuration) { 
//            self.view.layoutIfNeeded()
//        }
        
        //usingSpringWithDamping  弹簧明显程度，0-1 数值越小，弹簧效果越明显
        //initialSpringVelocity 初速度
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: {
            
            //snp_updateConstraints更新约束
            self.iconView.snp_updateConstraints { (make) in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height-160))
            }
            
            //autolayout动画必须调用view.layoutIfNeeded()，会根据约束重新设置frame
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(HYYDefaultDuration, animations: { 
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) in
                        print("动画完成,切换界面")
                        //切换界面
//                        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
//                        appDelegate?.window?.rootViewController = HYYMainViewController()
//                        appDelegate?.swithRootViewController(HYYMainViewController())
                        AppDelegate.outSwitchRootViewController(HYYMainViewController())
                })
        }
        
    }
    
    //MARK: - 懒加载
     /// 背景
    private lazy var bkgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
     /// 头像
    private lazy var iconView:UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    private lazy var welcomeLabel : UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    }()
 
}
